<?php
class Admin_AdminProductsController extends  Zendvn_Controller_Action{
	
	private $_paning = array('rowCount'=> 20, 'pageRange'=> 3);
	
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin/white');		
		$this->_templateObj->load();	
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		//$ss->unsetAll();
		if(empty($ss->col)){
			$ss->col = 'id';
			$ss->order = 'DESC';
		}
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_paning['rowCount'] = $this->_configObj->getSite('list_limit');		
		$this->_paning['pageRange'] = 3;				
		$this->_arrParam['panigator'] = $this->_paning;
		
		
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
		$this->view->Title = $this->view->translate('Mannger') . ' : '
							 .  $this->view->translate('Products') . ' : '
							 .  $this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
		
		$tblItem 					= new Zendvn_Model_Table_Product();
		$this->view->Items 			= $tblItem->listItem($this->_arrParam);
		$tblProductCategories 		= new Zendvn_Model_Table_ProductCategories();
		$this->view->slbParents 	= $tblProductCategories->itemInSelect($this->_arrParam,array('task'=>'product'));
		$totalItem 					= $tblItem->totalItem($this->_arrParam);
		$panigator 					= Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);		
		$this->view->panigator 		= $panigator;
		$this->view->slbSearchFiels = $tblItem->_searchFields;
		
	}
	
	public function addAction(){
		
		$this->view->Title = $this->view->translate('Manager') . ' : '
							 .  $this->view->translate('Products') . ' : '
							 .  $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		
		$this->view->headLink()->appendStylesheet(SCRIPTS_URL . "/uploadify/uploadify.css",'screen');
		$this->view->headScript()->appendFile(SCRIPTS_URL . "/uploadify/swfobject.js",'text/javascript');
		$this->view->headScript()->appendFile(SCRIPTS_URL . "/uploadify/jquery.uploadify.v2.1.4.min.js",'text/javascript');
		
		$tblItem 					= new Zendvn_Model_Table_Product();	
		$tblProductCategories 		= new Zendvn_Model_Table_ProductCategories();
		$this->view->slbParents 	= $tblProductCategories->itemInSelect($this->_arrParam,array('task'=>'product'));
		
		$tblSize 					= new Zendvn_Model_Table_ProductSize();
		$this->view->slbSize 		= $tblSize->itemInSelect($this->_arrParam);
		
		$tblColor 					= new Zendvn_Model_Table_ProductColor();
		$this->view->slbColor 		= $tblColor->itemInSelect($this->_arrParam);
		
		if($this->_request->isPost()){	

			$validator = new Admin_Form_ValidateProduct($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();
			}else{
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successfull!'));
				$data =  $validator->getData();	
				
				$data['hidden_size'] = json_decode(str_replace('\"', '"', $data['hidden_size']), true);
				$data['hidden_color'] = json_decode(str_replace('\"', '"', $data['hidden_color']), true);
				$data['hidden_stock'] = json_decode(str_replace('\"', '"', $data['hidden_stock']), true);
				
				$tblItem->saveItem($data,array('task'=>'admin-add'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController . '/' . $this->_arrParam['action']);
				}
			}		
		}
		$this->_helper->viewRenderer('form');
	}
	
	public function editAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							 .  $this->view->translate('Product') . ' : '
							 .  $this->view->translate('Add');
		$this->view->headLink()->appendStylesheet(SCRIPTS_URL . "/uploadify/uploadify.css",'screen');
		$this->view->headScript()->appendFile(SCRIPTS_URL . "/uploadify/swfobject.js",'text/javascript');
		$this->view->headScript()->appendFile(SCRIPTS_URL . "/uploadify/jquery.uploadify.v2.1.4.min.js",'text/javascript');
		
		$this->view->headTitle($this->view->Title,true);
		
		$tblItem 					= new Zendvn_Model_Table_Product();	
		$this->view->Item 			= $tblItem->getItem($this->_arrParam);
		$tblProductCategories 		= new Zendvn_Model_Table_ProductCategories();
		$this->view->slbParents 	= $tblProductCategories->itemInSelect($this->_arrParam,array('task'=>'product'));
		
		$tblSize 					= new Zendvn_Model_Table_ProductSize();
		$this->view->slbSize 		= $tblSize->itemInSelect($this->_arrParam);
		
		$tblColor 					= new Zendvn_Model_Table_ProductColor();
		$this->view->slbColor 		= $tblColor->itemInSelect($this->_arrParam, array('task' => 'get-select'));
		
		if($this->_request->isPost()){			
			$validator = new Admin_Form_ValidateProduct($this->_arrParam);		
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();
			}else{
				
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successfull!'));
				$data =  $validator->getData();		
							
				$data['hidden_size'] = json_decode(str_replace('\"', '"', $data['hidden_size']), true);
				$data['hidden_color'] = json_decode(str_replace('\"', '"', $data['hidden_color']), true);
				$data['hidden_stock'] = json_decode(str_replace('\"', '"', $data['hidden_stock']), true);
				
				$tblItem->saveItem($data,array('task'=>'admin-edit'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController 
									 . '/' . $this->_arrParam['action'] 
									 . '/id/' . $this->_arrParam['id']);
				}
			}			
		}
		$this->_helper->viewRenderer('form');
	}
	public function deleteAction(){
				
		$tblItem = new Zendvn_Model_Table_Product();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblItem->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tblItem->deleteItem($this->_arrParam);
		}
		$this->_helper->flashMessenger->addMessage(array('success'=>'The delete was successfull!'));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
		
	}
	public function infoAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							 .  $this->view->translate('Product') . ' : '
							 .  $this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		$tblItem = new Zendvn_Model_Table_Product();
		$this->view->Item = $tblItem->getItem($this->_arrParam,array('task'=>'info'));
	}
	
	public function statusAction(){
		$tblItem = new Zendvn_Model_Table_Product();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblItem->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tblItem->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function specialAction(){
		$tblItem = new Zendvn_Model_Table_Product();
		$special = ($this->_arrParam['s'] == 'active' )? 'inactive':'active';
		$data = array('special' => $special);
		$where = 'id = ' . $this->_arrParam['id'];
		$tblItem->update($data, $where);

		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function homePageAction(){
		$tblItem = new Zendvn_Model_Table_Product();
		$special = ($this->_arrParam['s'] == 'active' )? 'inactive':'active';
		$data = array('home_page' => $special);
		$where = 'id = ' . $this->_arrParam['id'];
		$tblItem->update($data, $where);

		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function filterAction(){
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}
		
		if($this->_arrParam['type'] == 'filter'){
			$ss->status = $this->_arrParam['status'];
			$ss->cat_id = $this->_arrParam['cat_id'];
		}
		
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function sortAction(){		
		
		if($this->_request->isPost()){
			$tblItem = new Zendvn_Model_Table_Product();
			$tblItem->sortItem($this->_arrParam);
			$this->_redirect($this->_mainAction);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	public function removePicAction(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
													 	  'fileConfig'=>'product-category.ini'));
		$itemConfig = $config->getConfig('item');
		$tblProduct = new Zendvn_Model_Table_Product();
		$upload = new Zendvn_File_Upload();
		
		if($this->_arrParam['type'] == 'image'){		
			$upload->remove($itemConfig['uploadDir'],$this->_arrParam['file']);
			$upload->remove($itemConfig['thumbDir1'],$this->_arrParam['file']);			
			$upload->remove($itemConfig['thumbDir2'],$this->_arrParam['file']);			
			$tblProduct->update(array('image'=>''), 'id = ' . $this->_arrParam['id']);
		}
		
		if($this->_arrParam['type'] == 'uploadify'){		
			$upload->remove($itemConfig['uploadDir'],$this->_arrParam['file']);
			$upload->remove($itemConfig['thumbDir1'],$this->_arrParam['file']);			
			$upload->remove($itemConfig['thumbDir2'],$this->_arrParam['file']);			
			$tblProduct->removeImages($this->_arrParam);
		}
		
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}	
	
	public function uploadifyAction(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
													'fileConfig'=>'product-category.ini'));
		$imageConfig = $config->getConfig('item');
		$upload = new Zendvn_File_Upload();
		if(!empty($_FILES)){
			$options = $imageConfig['extension'];
			$upload->addValidator('Extension',true,$options,'picture')
					->addValidator('Size',true,$imageConfig['size'],'picture')
					->addValidator('ImageSize',true,$imageConfig['imageSize'], 'picture');
				
			if(!$upload->isValid('picture')){
				echo 'error';
			}else{
			 	$options = array('custom_rename'=> true,'uploadDir'=> $imageConfig['uploadDir']);
				$fileName = $upload->upload($imageConfig['uploadDir'],'picture',$options);
				$arrParam = array(
								'uploadDir' => $imageConfig['uploadDir'],
								'thumb-width' => array($imageConfig['thumb-width1'], $imageConfig['thumb-width2']),
								'thumb-height' => array( $imageConfig['thumb-height1'], $imageConfig['thumb-height2']),
								'thumbDir'	=> array($imageConfig['thumbDir1'], $imageConfig['thumbDir2'])
								);
				$options = array('type' => array($imageConfig['thumbResize1'], $imageConfig['thumbResize2']));
				$img = Zendvn_File_Image::resizeImg($fileName, $arrParam, $options);
				
				$this->_arrParam['images'] = $fileName;
				
				echo $fileName;
			}
			$this->_helper->viewRenderer->setNoRender(); 
		}
		$this->_helper->layout->disableLayout();
	}
	
	public function enterStockAction(){
		$tblSize 					= new Zendvn_Model_Table_ProductSize();
		$this->view->slbSize 		= $tblSize->itemInSelect($this->_arrParam, array('task' => 'get-select'));
		
		$tblColor 					= new Zendvn_Model_Table_ProductColor();
		$this->view->slbColor 		= $tblColor->itemInSelect($this->_arrParam, array('task' => 'get-select'));
		
		
		if($this->_request->isPost()){	
			$response 	= array('type' => 'success', 'msg' => json_encode($this->_arrParam['stock']));
			echo json_encode($response);
			$this->_helper->viewRenderer->setNoRender();
		}
		
		$this->_helper->layout->disableLayout();
	}
	
	public function enterColorAction(){
		$tblSize 					= new Zendvn_Model_Table_ProductSize();
		$this->view->slbSize 		= $tblSize->itemInSelect($this->_arrParam);
		
		$tblColor 					= new Zendvn_Model_Table_ProductColor();
		$this->view->slbColor 		= $tblColor->itemInSelect($this->_arrParam);
	
		$arrSize = array_values($this->view->slbSize);
	
	
		if($this->_request->isPost()){
			$this->_arrParam['size_id'] = urldecode($this->_arrParam['size_id']);
			$this->_arrParam['size_id'] = json_decode($this->_arrParam['size_id'], true);
			$validator = new Admin_Form_ValidateAddColor($this->_arrParam);
			if($validator->isVaild()){
				$arrError = $validator->getMessageErrors();
				$response 	= array('type' => 'error','msg'=>$arrError);
			}else{
				$strColor	= json_encode($this->_arrParam['color']);
				$response 	= array('type' => 'success', 'msg' => $strColor);
			}
			echo json_encode($response);
			$this->_helper->viewRenderer->setNoRender();
		}
	
		$this->_helper->layout->disableLayout();
	}
	
	public function jsonEncodeAction(){
		$tblItem = new Zendvn_Model_Table_Product();
		if(!empty($this->_arrParam['arrData'])){
			$arrData = explode('|', $this->_arrParam['arrData']);
			unset($arrData[count($arrData) - 1]);
			echo json_encode($arrData);
		}	
		
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

	}
}